package com.sen.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * getParameter family of methods 与getInputStream方法获取参数
 * Servlet规范：
 *    1. The request is an HTTP or HTTPS request.
 *    2. The HTTP method is POST.
 *    3. The content type is application/x-www-form-urlencoded.
 *    4. The servlet has made an initial call of any of the getParameter family of methods on the request object.
 * If the conditions are met, post form data will no longer be available for reading directly from the request object’s input stream.
 * 
 * 参考：http://hongjiang.info/http-application-x-www-form-urlencoded/
 */
public class TestServletGetInputStream extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TestServletGetInputStream() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 * 使用getParameter family of methods即可,可以多次获取同一参数
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    System.out.println("---"+request.getParameter("p1"));
	    Map<String,String[]> map = request.getParameterMap();
        for(String key : map.keySet()) {
            System.out.println("KEY:" + key );
            System.out.println("VALUE:" + map.get(key)[0].toString());
        }
		response.getWriter().print("GET ");
	}

	/**
	 * 注:
	 * 1、当请求头Content-Type类型是application/x-www-form-urlencoded时,所有参数(包括get和post)将被编码为key=value形式.
	 *   此时：post参数也得是key=value&key=value形式,没有=号的话,服务端获取的参数只有key没有value
	 *       getParameter family of methods 先执行可以获取所有参数(如果getInputStream()先执行后,这些方法只能获取get参数,post参数取不到)
	 *       getInputStream()只能获取一次post body参数(getParameter family of methods先执行后此方法将不能再获取参数)
	 * 
	 * 2、当请求头Content-Type类型是text/plain或text/html或application/json或application/xml时:
	 *       getParameter family of methods 只能获取get参数,可以多次获取同一参数
     *       getInputStream() 只能获取一次post body参数
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    //获取单个parameter
	    System.out.println("---"+request.getParameter("p1"));
	    System.out.println("---"+request.getParameter("p3"));
	    //获取parameter-map
	    Map<String,String[]> map = request.getParameterMap();
        for(String key : map.keySet()) {
            System.out.println("KEY:" + key );
            System.out.println("VALUE:" + map.get(key)[0].toString());
        }
        //使用is读取
        String text = null;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            char[] readBuffer = new char[1024];
            int n;
            StringBuffer buffer = new StringBuffer();
            while ((n = in.read(readBuffer)) > 0) {
                buffer.append(readBuffer, 0, n);
            }
            text = buffer.toString();
        }catch(Exception e) {
            e.printStackTrace();
        }
        response.getWriter().print("POST :" + text);
	}

}
